package com.nefu.softlab.xjwc.controller;

import com.nefu.softlab.xjwc.model.Classify;
import com.nefu.softlab.xjwc.model.Score;
import com.nefu.softlab.xjwc.model.vo.ScoreVo;
import com.nefu.softlab.xjwc.service.ScoreService;
import com.nefu.softlab.xjwc.utils.JSONResult;
import com.nefu.softlab.xjwc.utils.JsonUtil;
import com.nefu.softlab.xjwc.utils.LoggerUtil;
import com.nefu.softlab.xjwc.utils.XJWCException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Ar1es
 * @date : 2020/2/8
 * @since : Java 8
 */
@RestController
@Slf4j
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
public class ScoreController {

    private final LoggerUtil logger;
    private final ScoreService scoreService;

    @Autowired
    public ScoreController(LoggerUtil logger, ScoreService scoreService) {
        this.logger = logger;
        this.scoreService = scoreService;
    }


    /**
     * 专家查看自己对某一项目是否评分
     * @param score
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("doctor")
    @PostMapping("/scoreExist")
    public JSONResult selectScoreIsExist(@RequestBody Score score) throws XJWCException {
        if (ObjectUtils.isEmpty(score)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("GET selectScoreIsExist : " + JsonUtil.getJsonString(score));
        return JSONResult.ok(scoreService.selectScoreIsExist(score));
    }

    /**
     * 管理员查看项目各个专家的评分
     * @param score
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("admin")
    @PostMapping("/scoreList")
    public JSONResult selectScoreList(@RequestBody Score score) throws XJWCException{
        if (ObjectUtils.isEmpty(score)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("GET selectScoreList : " + JsonUtil.getJsonString(score));
        return JSONResult.ok(scoreService.selectScoreByProject(score));
    }

    /**
     * 管理员查看项目有几个专家评分
     * @param score
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("admin")
    @PostMapping("/scoreNumber")
    public JSONResult selectScoreNumberByProject(@RequestBody Score score) throws XJWCException{
        if (ObjectUtils.isEmpty(score)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("GET selectScoreNumberByProject : " + JsonUtil.getJsonString(score));
        return JSONResult.ok(scoreService.selectScoreNumberByProject(score));
    }

    /**
     * 管理员查看项目是否所有人员评分完成
     * @param score
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("admin")
    @PostMapping("/scoreOver")
    public JSONResult selectScoreNumberIsOVer(@RequestBody Score score) throws XJWCException{
        if (ObjectUtils.isEmpty(score)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("GET selectScoreNumberIsOVer : " + JsonUtil.getJsonString(score));
        return JSONResult.ok(scoreService.selectScoreIsOver(score));
    }

    /**
     * 专家增加自己对某一项目的评分
     * @param score
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("doctor")
    @PostMapping("/score")
    public JSONResult insertScore(@RequestBody ScoreVo score) throws XJWCException{
        if (ObjectUtils.isEmpty(score)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        log.info("POST insertScore : " + JsonUtil.getJsonString(score));
        List<Classify> list = score.getTag();
        list.forEach(c -> {
            if (ObjectUtils.isEmpty(c.getValue()) || "".equals(c.getValue())) {
                c.setValue(0);
            }
        });

        return JSONResult.ok(scoreService.insertScore(score));
    }

    /**
     * 专家修改自己对某一项目的评分
     * @param score
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("doctor")
    @PutMapping("/score")
    public JSONResult updateScore(@RequestBody ScoreVo score) throws XJWCException{
        if (ObjectUtils.isEmpty(score)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("PUT updateScore : " + JsonUtil.getJsonString(score));
        return JSONResult.ok(scoreService.updateScore(score));
    }


    /**
     * 那个坑爹的算法
     * @param
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("admin")
    @PostMapping("/calculate")
    public JSONResult calculate(@RequestBody Score score) throws XJWCException{
        logger.info("GET calculate : ");
        return JSONResult.ok(scoreService.calculateScore(score));
    }

    /**
     * 删除某一项目评分（暂时用不到）
     * @param score
     * @return
     * @throws XJWCException
     */
    @DeleteMapping("/score")
    public JSONResult deleteScore(@RequestBody Score score) throws XJWCException{
        if (ObjectUtils.isEmpty(score)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("DELETE deleteScore : " + JsonUtil.getJsonString(score));
        return JSONResult.ok(scoreService.deleteScore(score));
    }


}
